﻿using MHI.ScenarioManager.Interfaces;
using WebFoundation;
using WebFoundation.GenericModules.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.Http;

namespace MHI.ScenarioManager.WebApi
{
    [RoutePrefix("api/ScenarioManager")]
    public class ScenarioManagerController : WFApiController
    {
        [WFAutoLoad]
        public IScenarioModule module { get; set; }

        /// <summary>
        /// 根据id获取方案
        /// </summary>
        /// <param name="scenarioId">方案Id</param>
        /// <returns>方案</returns>
        [HttpGet]
        [Route("GetScenarioById/")]
        [Description("根据id获取方案")]
        public Scenario GetScenarioById([FromUri] Guid scenarioId)
        {
            Scenario scenario = module.Scenarios.Get(scenarioId);
            if (scenario == null)
            {
                return null;
            }
            return scenario;
        }

        /// <summary>
        /// 根据Code获取方案
        /// </summary>
        /// <param name="scenarioCode">方案Code</param>
        /// <returns>方案</returns>
        [HttpGet]
        [Route("GetScenarioByCode/")]
        [Description("根据方案Code获取方案")]
        public Scenario GetScenarioByCode(string scenarioCode)
        {
            //Guid sId = Guid.Parse(scenarioId);
            Scenario scenario = module.Scenarios.GetScenario(scenarioCode);
            if (scenario == null)
            {
                return null;
            }
            return scenario;
        }

        /// <summary>
        /// 获取所有ScenarioGroup根节点
        /// </summary>
        /// <returns>Group组</returns>
        [HttpGet]
        [Route("GetRootScenarioGroupList/")]
        [Description("获取所有ScenarioGroup根节点")]
        public List<Group> GetRootScenarioGroupList()
        {
            return module.ScenarioGroups.GetRootScenarioGroups().ToList<Group>();
        }

        /// <summary>
        /// 获取所有ScenarioGroup子节点
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetChildScenarioGroupList/")]
        [Description("获取所有ScenarioGroup子节点")]
        public List<Group> GetChildScenarioGroupList(string parentGroupId)
        {
            //List<Group> groupList = new List<Group>();
            Guid gId = Guid.Parse(parentGroupId);
            Group parentGroup = module.ScenarioGroups.Get(gId);
            if (parentGroup == null)
            {
                return null;
            }
            return module.ScenarioGroups.GetScenarioGroupsByGroup(parentGroup);//.ToArray();
            //for (int index = 0; index < groups.Length; index++)
            //{
            //    Group group = groups[index];
            //    Scenario scenario = module.Scenarios.Get(group.ItemId);
            //    if (scenario==null)
            //    {
            //        groupList.Add(group);
            //    }
            //}
            //return groupList;
        }

        /// <summary>
        /// 获取所有TemplateGroup节点
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetTemplateGroupList/")]
        [Description("获取所有TemplateGroup节点")]
        public List<Group> GetTemplateGroupList(string groupId)
        {
            List<Group> groupList = new List<Group>();
            Guid gId = new Guid(groupId);
            Group[] groups = module.ScenarioGroups.GetSubGroups(gId).ToArray();
            for (int index = 0; index < groups.Length; index++)
            {
                Group group = groups[index];
                Scenario scenario = module.Scenarios.Get(group.ItemId);
                if (scenario != null && scenario.Template == 1)
                {
                    groupList.Add(group);
                }
            }
            return groupList;
        }

        /// <summary>
        /// 根据group获取组里所有方案
        /// </summary>
        /// <param name="groupId">groupId</param>
        /// <returns>Scenario组</returns>
        [HttpGet]
        [Route("GetScenarioListByGroup/")]
        [Description("根据group获取组里所有方案")]
        public Scenario[] GetScenarioListByGroup(string groupId)
        {
            Guid gId = new Guid(groupId);
            Group group = module.ScenarioGroups.Get(gId);
            if(group==null)
            {
                return null;
            }
            return module.ScenarioGroups.GetScenariosByGroup(group).ToArray();
        }

        /// <summary>
        /// 获取所有模板方案
        /// </summary>
        /// <returns>Scenario组</returns>
        [HttpGet]
        [Route("GetTemplateScenarioList/")]
        [Description("获取所有模板方案")]
        public Scenario[] GetTemplateScenarioList()
        {
            return module.Scenarios.GetTemplateScenarios().ToArray();
        }

        /// <summary>
        /// 根据方案子类获取所有该类型方案
        /// </summary>
        /// <param name="subType">方案子类</param>
        /// <returns>Scenario组</returns>
        [HttpGet]
        [Route("GetScenarioListBySubType/")]
        [Description("根据方案子类获取所有该类型方案")]
        public Scenario[] GetScenarioListBySubType(string subType)
        {
            return module.Scenarios.GetScenarioByModelSubType(subType).ToArray();
        }

        [HttpGet]
        [Route("GetChildScenarioListByScenario/")]
        [Description("根据方案ID获取所有该方案的子方案")]
        public Scenario[] GetChildScenarioListByScenario(Guid scenarioId)
        {
            return module.Scenarios.GetChildScenarioListByScenario(scenarioId).ToArray();
        }

        /// <summary>
        /// 创建根方案组
        /// </summary>
        /// <param name="groupName">方案组名称</param>
        /// <returns></returns>
        [HttpPost]
        [Route("CreateRootScenarioGroup/")]
        [Description("创建根方案组")]
        public Group CreateRootScenarioGroup(CreateRootScenarioGroupPara para)
        {
            return module.ScenarioGroups.CreateRootScenarioGroup(para.GroupName);
        }

        /// <summary>
        /// 创建子级方案组
        /// </summary>
        /// <param name="parentId">父级方案组Id</param>
        /// <param name="groupName">方案组名称</param>
        /// <returns></returns>
        [HttpPost]
        [Route("CreateScenarioGroup/")]
        [Description("创建子级方案组")]
        public Group CreateScenarioGroup(CreateScenarioGroupPara para)
        {
            Group parent = module.ScenarioGroups.Get(para.ParentId);
            if (parent == null)
            {
                return null;
            }
            return module.ScenarioGroups.CreateScenarioGroup(parent, para.GroupName);
        }

        /// <summary>
        /// 创建模板方案
        /// </summary>
        /// <param name="modelFile">模型在服务端的绝对路径</param>
        /// <param name="groupId">所属方案组Id</param>
        /// <param name="newTemplateName">模板方案名称</param>
        /// <param name="subType">方案子类</param>
        /// <param name="description">方案描述</param>
        /// <returns></returns>
        [HttpPost]
        [Route("CreateTemplateScenario/")]
        [Description("创建模板方案")]
        public Scenario CreateTemplateScenario(CreateTemplateScenarioPara para)
        {
            Guid groupId = new Guid(para.GroupId);
            return module.CreateTemplateScenario(para.ModelFile, groupId, para.NewTemplateName, para.SubType.ToString(), para.Description);
        }

        /// <summary>
        /// 创建新方案
        /// </summary>
        /// <param name="parentScenarioId">父级方案Id</param>
        /// <param name="groupId">组Id</param>
        /// <param name="newScenarioName">新方案名称</param>
        /// <param name="startTime">设置模拟开始时间</param>
        /// <param name="endTime">设置模拟结束时间</param>
        /// <param name="subType">方案子类型</param>
        /// <param name="description">方案说明</param>
        /// <returns></returns>
        [HttpPost]
        [Route("CreateScenario/")]
        [Description("创建方案")]
        public Scenario CreateScenario(CreateScenarioPara para)
        {
            Guid scenarioId = new Guid(para.ParentScenarioId);
            Scenario parentScenario = module.Scenarios.Get(scenarioId);
            if(parentScenario==null)
            {
                this.GetType().LogInfo(para.ParentScenarioId.ToString());
                return null;
            }
            Group group = module.ScenarioGroups.GetGroupByScenario(parentScenario);
            if(group==null)
            {
                return null;
            }

            return module.CreateScenario(parentScenario.Id, group.Id, para.NewScenarioName, para.StartTime.ToString(), para.EndTime.ToString(), para.SubType.ToString(), para.Description);
        }

        [HttpPost]
        [Route("CreateScenarioByGroup/")]
        [Description("指定方案组创建方案")]
        public Scenario CreateScenarioByGroup(CreateScenarioByGroupPara para)
        {
            Guid scenarioId = new Guid(para.ParentScenarioId);
            Scenario parentScenario = module.Scenarios.Get(scenarioId);
            if (parentScenario == null)
            {
                this.GetType().LogInfo(para.ParentScenarioId.ToString(), "parentScenario is null!");
                return null;
            }
            Guid groupId = new Guid(para.GroupId);
            Group group = module.ScenarioGroups.Get(groupId);
            if (group == null)
            {
                this.GetType().LogInfo(para.GroupId.ToString(),"group is null!");
                return null;
            }

            return module.CreateScenario(parentScenario.Id, group.Id, para.NewScenarioName, para.StartTime.ToString(), para.EndTime.ToString(), para.SubType.ToString(), para.Description);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("CreateScenarioFromFile/")]
        [Description("创建方案")]
        public Scenario CreateScenarioFromFile(CreateScenarioFromFilePara para)
        {
            Guid groupId = new Guid(para.GroupId);
            return module.CreateScenarioFromFile(para.ModelFile, groupId, para.NewScenarioName,para.SubType.ToString(), para.Description);
        }

        [HttpPost]
        [Route("DeleteScenario/")]
        [Description("删除方案")]
        public bool DeleteScenario(DeleteScenarioPara para)
        {
            Guid scenarioId = new Guid(para.ScenarioId);
            Scenario scenario = module.Scenarios.Get(scenarioId);
            if (scenario != null)
            {
                return module.DeleteScenarioAndModel(scenario);
            }
            return  false;
        }

        [HttpPost]
        [Route("RenameScenario/")]
        [Description("重命名方案")]
        public Scenario RenameScenario(RenameScenarioPara para)
        {
            return module.Scenarios.Rename(para.OldScenarioCode,para.NewName);
        }

        [HttpPost]
        [Route("DeleteScenarioGroup/")]
        [Description("删除方案")]
        public bool DeleteScenarioGroup(DeleteScenarioGroupPara para)
        {
            Guid scenarioGroupId = new Guid(para.ScenarioGroupId);
            Group scenarioGroup = module.ScenarioGroups.Get(scenarioGroupId);
            if (scenarioGroup != null)
            {
                return module.DeleteScenarioGroupAndScenario(scenarioGroup);
            }
            return false;
        }

        [HttpPost]
        [Route("RenameScenarioGroup/")]
        [Description("重命名方案组")]
        public Group RenameScenarioGroup(RenameScenarioGroupPara para)
        {
            Guid scenarioGroupId = new Guid(para.OldScenarioGroupId);
            Group scenarioGroup = module.ScenarioGroups.Get(scenarioGroupId);
            if (scenarioGroup != null)
            {
                return module.ScenarioGroups.Rename(scenarioGroup, para.NewGroupName);
            }
            return null;
        }


        //[HttpPost]
        //[Route("UpdateScenarioAndModel/")]
        //[Description("更新方案和模型")]
        //public bool UpdateScenarioAndModel(Scenario scenario)
        //{
        //    return module.UpdateScenarioAndModel(scenario);
        //}

        [HttpGet]
        [Route("GetScenarioCalculatedStatus/")]
        [Description("获取方案计算状态")]
        public bool GetScenarioCalculatedStatus(string scenarioCode)
        {
            return module.Scenarios.IsCalculated(scenarioCode);
        }
    }

    public class RenameScenarioPara
    {
        public string OldScenarioCode
        {
            get;
            set;
        }

        public string NewName
        {
            get;
            set;
        }
    }

    public class RenameScenarioGroupPara
    {
        public string OldScenarioGroupId
        {
            get;
            set;
        }

        public string NewGroupName
        {
            get;
            set;
        }
    }

    public class CreateRootScenarioGroupPara
    {
        public CreateRootScenarioGroupPara()
        {
            GroupName = string.Empty;
        }

        public string GroupName
        {
            get;
            set;
        }
    }

    public class CreateScenarioGroupPara
    {
        public CreateScenarioGroupPara()
        {
            ParentId = Guid.Empty;
            GroupName = string.Empty;
        }

        public Guid? ParentId
        {
            get;
            set;
        }

        public string GroupName
        {
            get;
            set;
        }
    }

    public class CreateTemplateScenarioPara
    {
        public CreateTemplateScenarioPara()
        {
            ModelFile = string.Empty;
            GroupId = Guid.Empty.ToString();
            NewTemplateName = string.Empty;
            SubType = enumSubType.Unknown;
            Description = string.Empty;
        }
        public string ModelFile
        {
            get;
            set;
        }
        public string GroupId
        {
            get;
            set;
        }
        public string NewTemplateName
        {
            get;
            set;
        }

        public enumSubType SubType
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }
    }

    public class CreateScenarioPara
    {
        public CreateScenarioPara()
        {
            ParentScenarioId = Guid.Empty.ToString();
            NewScenarioName = string.Empty;
            StartTime = DateTime.Now;
            EndTime = DateTime.Now;
            SubType = enumSubType.Unknown;
            Description = string.Empty;
        }
        public string ParentScenarioId
        {
            get;
            set;
        }
        public string NewScenarioName
        {
            get;
            set;
        }
        public DateTime StartTime
        {
            get;set;
        }
        public DateTime EndTime
        {
            get;set;
        }
        public enumSubType SubType
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

    }

    public class CreateScenarioByGroupPara
    {
        public CreateScenarioByGroupPara()
        {
            ParentScenarioId = Guid.Empty.ToString();
            NewScenarioName = string.Empty;
            StartTime = DateTime.Now;
            EndTime = DateTime.Now;
            SubType = enumSubType.Unknown;
            Description = string.Empty;
        }
        public string ParentScenarioId
        {
            get;
            set;
        }

        public string GroupId
        {
            get;
            set;
        }

        public string NewScenarioName
        {
            get;
            set;
        }
        public DateTime StartTime
        {
            get;
            set;
        }
        public DateTime EndTime
        {
            get;
            set;
        }
        public enumSubType SubType
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

    }

    public class CreateScenarioFromFilePara
    {
        public CreateScenarioFromFilePara()
        {
            ModelFile = string.Empty;
            GroupId = Guid.Empty.ToString();
            NewScenarioName = string.Empty;
            SubType = enumSubType.Unknown;
            Description = string.Empty;
        }
        public string ModelFile
        {
            get;
            set;
        }

        public string GroupId
        {
            get;
            set;
        }

        public string NewScenarioName
        {
            get;
            set;
        }

        public enumSubType SubType
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

    }

    public class DeleteScenarioPara
    {
        public DeleteScenarioPara()
        {
            ScenarioId = Guid.Empty.ToString();
        }

        public string ScenarioId
        {
            get;
            set;
        }
    }

    public class DeleteScenarioGroupPara
    {
        public DeleteScenarioGroupPara()
        {
            ScenarioGroupId = Guid.Empty.ToString();
        }

        public string ScenarioGroupId
        {
            get;
            set;
        }
    }
}
